<?php

class labmanager
{
	public $client;
	public $debug_mode;
	
	public function debugXMLDump(&$request, &$client)
	{
		global $debug_mode;
		
		if ($debug_mode == true)
		{
			$request = $client->__getLastRequestHeaders();
			file_put_contents('headers.xml', $request);

			$request = $client->__getLastRequest(); 
			file_put_contents('request.xml', $request);

			$request = $client->__getLastResponse(); 
			file_put_contents('response.xml', $request);
		}
	}
	
	function __construct($hostname, $username, $password, $organization, $workspacename, $debug=false)
	{
		global $client;
		global $debug_mode;
		$debug_mode = $debug;
		
		unset($soap_dat);
		
		$soap_dat["username"] = $username;
		$soap_dat["password"] = $password;
		$soap_dat["organizationname"] = $organization;
		$soap_dat["workspacename"] = $workspacename;

		$namespace = "http://vmware.com/labmanager";

		if ($debug == true)
		{
			$client = new SoapClient("https://$hostname/LabManager/SOAP/LabManager.asmx?wsdl", array('trace' => 1));
		}
		else
		{
			$client = new SoapClient("https://$hostname/LabManager/SOAP/LabManager.asmx?wsdl");
		}

		$authvar = new SoapVar($soap_dat, SOAP_ENC_OBJECT);
		
		$header = new SOAPHeader($namespace, 'AuthenticationHeader', $soap_dat);

		$client->__setSoapHeaders($header);
	}
	
	public function printContainer($obj)
	{
		foreach ($obj as $key => $value)
		{
			echo "<strong>$key</strong>: $value <br/>";
		}
	}
	
	public function ConfigurationCapture($value='')
	{
		return "not implemented yet";
	}	
	public function ConfigurationCheckout($configurationId, $workspaceName)
	{
		global $client;
		
		$soap_req["configurationId"] = $configurationId;
		$soap_req["workspaceName"] = $workspaceName;
		
		try
		{
			$result = $client->ConfigurationCheckout($soap_req);
		}
		catch (SoapFault $e)
		{
			echo $e->getMessage();
			return NULL;
		}

		return $result->ConfigurationCheckoutResult;
	}	
	public function ConfigurationClone($configurationId, $newWorkspaceName)
	{
		global $client;
		
		$soap_req["configurationId"] = $configurationId;
		$soap_req["newWorkspaceName"] = $newWorkspaceName;
		
		try
		{
			$result = $client->ConfigurationClone($soap_req);
		}
		catch (SoapFault $e)
		{
			echo $e->getMessage();
			return NULL;
		}

		return $result->ConfigurationCloneResult;
	}	
	public function ConfigurationDelete($value='')
	{
		return "not implemented yet";
	}	
	public function ConfigurationDeploy($configurationId, $isCached=false, $fenceMode=1)
	{
		global $client;
		
		$soap_req["configurationId"] = $configurationId;
		$soap_req["isCached"] = $isCached;
		$soap_req["fenceMode"] = $fenceMode;
		
		try
		{
			$result = $client->ConfigurationDeploy($soap_req);
		}
		catch (SoapFault $e)
		{
			echo $e->getMessage();
				$request = $client->__getLastRequestHeaders();
				file_put_contents('headers.xml', $request);

				$request = $client->__getLastRequest(); 
				file_put_contents('request.xml', $request);

				$request = $client->__getLastResponse(); 
				file_put_contents('response.xml', $request);
			return NULL;
		}
		
		$this->debugXMLDump($result, $client);
		
	}	
	public function ConfigurationPerformAction($value='')
	{
		return "not implemented yet";
	}	
	public function ConfigurationSetPublicPrivate($value='')
	{
		return "not implemented yet";
	}	
	public function ConfigurationUndeploy($value='')
	{
		return "not implemented yet";
	}	
	public function GetConfiguration($value='')
	{
		return "not implemented yet";
	}
	public function GetConfigurationByName($value)
	{
		global $client;
		try
		{
			$result = $client->GetConfigurationByName(array("name" => $value));
		}
		catch (SoapFault $e)
		{
			echo $e->getMessage();
			
			
			$request = $client->__getLastRequestHeaders();
			file_put_contents('headers.xml', $request);

			$request = $client->__getLastRequest(); 
			file_put_contents('request.xml', $request);

			$request = $client->__getLastResponse(); 
			file_put_contents('response.xml', $request);
			
			
			
			return NULL;
		}
		return $result;
	}
	
	/*Non-Standard Addition*/
	public function GetConfigurationIdByName($value)
	{
		$configuration = $this->GetConfigurationByName($value);
		
		return $configuration->GetConfigurationByNameResult->Configuration->id;
	}
	
	public function GetCurrentOrganizationName()
	{
		global $client;
		try
		{
			$result = $client->__soapCall('GetCurrentOrganizationName', array());
		}
		catch (SoapFault $e)
		{
			echo $e->getMessage();
			return NULL;
		}
		$vars = get_object_vars($result);
		
		return ($vars["GetCurrentOrganizationNameResult"]);
	}
	public function GetCurrentWorkspaceName()
	{
		global $client;
		try
		{
			$result = $client->GetCurrentWorkSpaceName(array());
		}
		catch (SoapFault $e)
		{
			echo $e->getMessage();
			return NULL;
		}
		return $result->GetCurrentWorkspaceNameResult;
	}	
	public function GetMachine($value='')
	{
		return "not implemented yet";
	}	
	public function GetMachineByName($value='')
	{
		return "not implemented yet";
	}	
	public function GetSingleConfigurationByName($value='')
	{
		return "not implemented yet";
	}

	public function printVariables()
	{
		global $client;
		$this->printContainer($client->__getTypes());
	}
	public function listFunctions()
	{
		global $client;
		$this->printContainer($client->__getFunctions());
	}
		
	public function ListConfigurations($value)
	{
		global $client;
		
		try
		{
				$result = $client->ListConfigurations(array("configurationType" => $value));
		}
		catch (SoapFault $e)
		{
			echo $e->getMessage();		
			//$this->debugXMLDump($result,$client);			
			return NULL;
		}
		
		if (is_array($result->ListConfigurationsResult->Configuration))
		{
			foreach ($result->ListConfigurationsResult->Configuration as $config)
			{
				$this->printContainer($config);
			}
		}
		else
		{
			foreach ($result->ListConfigurationsResult as $config)
			{
				$this->printContainer($config);
			}
		}
	}	
	public function ListMachines($value)
	{
		global $client;
		
		try
		{
			$result = $client->ListMachines(array("configurationId" => $value));
		}
		catch (SoapFault $e)
		{
			echo $e->getMessage();		
			return NULL;
		}
			
		if (is_array($result->ListMachinesResult->Machine))
		{
			foreach ($result->ListMachinesResult->Machine as $config)
			{
				$this->printContainer($config);
			}
		}
		else
		{
			foreach ($result->ListMachinesResult as $config)
			{
				$this->printContainer($config);
			}
		}
	}	
	public function LiveLink($value)
	{
			global $client;

			$soap_req["configName"] = $value;

			try
			{
				$result = $client->LiveLink($soap_req);
			}
			catch (SoapFault $e)
			{
				echo $e->getMessage();
				return NULL;
			}

			return $result->LiveLinkResult;
	}	
	public function MachinePerformAction($value='')
	{
		return "not implemented yet";
	}	
	public function SetCurrentOrganizationByName($value='')
	{
		return "not implemented yet";
	}	
	public function SetCurrentWorkspaceByName($value='')
	{
		return "not implemented yet";
	}

}

?>